package con.shake;

import java.util.List;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.app.ActivityManager.RunningServiceInfo;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.location.Location;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

public class ShakeApp extends Activity {
	/** Called when the activity is first created. */
	private boolean isServiceRunning;
	private boolean isServiceRunningValue;
	private static final int PICK_CONTACT = 0;

	public static final String KEY_IS_SERVICE_RUNNING = "KEY_IS_SERVICE_RUNNING";
	public static final String KEY_IS_3G_ENABLED = "KEY_IS_3G_ENABLED";
	public static final String KEY_IS_WIFI_ENABLED = "KEY_IS_WIFI_ENABLED";
	public static final String KEY_IS_CALL_ENABLED = "KEY_IS_CALL_ENABLED";

	public static final String KEY_CONTACT_NUMBER = "KEY_CONTACT_NUMBER";
	private static final String KEY_CONTACT_NAME = "KEY_CONTACT_NAME";

	private static final String START = "Start";
	private static final String STOP = "Stop";

	private String contactName;
	private String contactNo;

	private CheckBox checkBox;
	private Button button3g;
	private Button buttonWifi;
	private Button buttonCall;

	private TextView fieldContactNo;
	private TextView fieldContactName;

	private SharedPreferences sharedPreference;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		try {
			super.onCreate(savedInstanceState);
			setContentView(R.layout.shake);
			sharedPreference = PreferenceManager
					.getDefaultSharedPreferences(this);
			//
			checkBox = (CheckBox) findViewById(R.id.check_box_start_stop_service);
			button3g = (Button) findViewById(R.id.button_direction_x);
			buttonWifi = (Button) findViewById(R.id.button_direction_y);
			buttonCall = (Button) findViewById(R.id.button_direction_z);
			fieldContactNo = (TextView) findViewById(R.id.field_contact_no);
			fieldContactName = (TextView) findViewById(R.id.field_contact_name);
			//
			isServiceRunningValue = sharedPreference.getBoolean(
					KEY_IS_SERVICE_RUNNING, false);
			isServiceRunning = Utils.isServiceRunning(ShakeApp.this, MyService.class);
			//
			if (isServiceRunningValue) {
				checkBox.setChecked(true);
				if (!isServiceRunning) {
					startService(new Intent(ShakeApp.this, MyService.class));
					isServiceRunning = true;
				}
			} else {
				checkBox.setChecked(false);
				if (isServiceRunning) {
					stopService(new Intent(ShakeApp.this, MyService.class));
					isServiceRunning = false;
				}
			}
			if (sharedPreference.getBoolean(KEY_IS_3G_ENABLED, false)) {
				button3g.setText(STOP);
			} else {
				button3g.setText(START);
			}
			if (sharedPreference.getBoolean(KEY_IS_WIFI_ENABLED, false)) {
				buttonWifi.setText(STOP);
			} else {
				buttonWifi.setText(START);
			}
			if (sharedPreference.getBoolean(KEY_IS_CALL_ENABLED, false)) {
				buttonCall.setText(STOP);
			} else {
				buttonCall.setText(START);
			}

			contactNo = sharedPreference.getString(KEY_CONTACT_NUMBER, null);
			contactName = sharedPreference.getString(KEY_CONTACT_NAME, null);
			if (contactNo != null) {
				fieldContactNo.setText(contactNo);
			} else {
				fieldContactNo.setText("Contact Number");
			}
			if (contactName != null) {
				fieldContactName.setText(contactName);
			} else {
				fieldContactName.setText("Contact Name");
			}

			checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				public void onCheckedChanged(CompoundButton cb, boolean value) {
					sharedPreference.edit()
							.putBoolean(KEY_IS_SERVICE_RUNNING, value).commit();
					if (value) {
						if (!isServiceRunning) {
							startService(new Intent(ShakeApp.this,
									MyService.class));
							isServiceRunning = true;
						}
					} else {
						if (isServiceRunning) {
							stopService(new Intent(ShakeApp.this,
									MyService.class));
							isServiceRunning = false;
						}
					}
				}
			});
			button3g.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if (START.equals(button3g.getText())) {
						button3g.setText(STOP);
						sharedPreference.edit()
								.putBoolean(KEY_IS_3G_ENABLED, true).commit();
					} else {
						button3g.setText(START);
						sharedPreference.edit()
								.putBoolean(KEY_IS_3G_ENABLED, false).commit();
					}

				}
			});
			buttonWifi.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if (START.equals(buttonWifi.getText())) {
						buttonWifi.setText(STOP);
						sharedPreference.edit()
								.putBoolean(KEY_IS_WIFI_ENABLED, true).commit();
					} else {
						buttonWifi.setText(START);
						sharedPreference.edit()
								.putBoolean(KEY_IS_WIFI_ENABLED, false)
								.commit();
					}

				}
			});
			buttonCall.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					if (START.equals(buttonCall.getText())) {
						if(contactName==null || contactNo == null){
							Intent intent = new Intent(Intent.ACTION_PICK,
									ContactsContract.Contacts.CONTENT_URI);
							startActivityForResult(intent, PICK_CONTACT);
						}
						buttonCall.setText(STOP);
						sharedPreference.edit()
								.putBoolean(KEY_IS_CALL_ENABLED, true).commit();
					} else {
						buttonCall.setText(START);
						sharedPreference.edit()
								.putBoolean(KEY_IS_CALL_ENABLED, false)
								.commit();
					}

				}
			});
			fieldContactNo.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Intent.ACTION_PICK,
							ContactsContract.Contacts.CONTENT_URI);
					startActivityForResult(intent, PICK_CONTACT);
				}
			});
			fieldContactName.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					Intent intent = new Intent(Intent.ACTION_PICK,
							ContactsContract.Contacts.CONTENT_URI);
					startActivityForResult(intent, PICK_CONTACT);
				}
			});
			// new AsyncTask<String, String, String>() {
			// protected void onPreExecute() {
			// try {
			//
			// } catch (Exception e) {
			// e.printStackTrace();
			// }
			//
			// }
			// protected String doInBackground(String... arg0) {
			// try {
			//
			//
			//
			// } catch (Exception e) {
			// e.printStackTrace();
			// }
			// return null;
			// }
			// protected void onPostExecute(String unused) {
			// try {
			//
			// } catch (Exception e) {
			// e.printStackTrace();
			// }
			// }
			// }.execute();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		switch (requestCode) {
		case (PICK_CONTACT):
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c = managedQuery(contactData, null, null, null, null);
				if (c.moveToFirst()) {
					String contactId = c.getString(c
							.getColumnIndex(ContactsContract.Contacts._ID));
					contactName = c
							.getString(c
									.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
					String hasPhone = c
							.getString(c
									.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

					if (hasPhone.equalsIgnoreCase("1")) {
						hasPhone = "true";
					} else {
						hasPhone = "false";
					}

					if (Boolean.parseBoolean(hasPhone)) {
						Cursor phones = getContentResolver()
								.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
										null,
										ContactsContract.CommonDataKinds.Phone.CONTACT_ID
												+ " = " + contactId, null, null);
						while (phones.moveToNext()) {
							contactNo = phones
									.getString(phones
											.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
						}
						phones.close();
					}
					if (contactNo != null) {
						fieldContactNo.setText(contactNo);
					} else {
						fieldContactNo.setText("Contact Number");
					}
					if (contactName != null) {
						fieldContactName.setText(contactName);
					} else {
						fieldContactName.setText("Contact Name");
					}
					sharedPreference.edit()
					.putString(KEY_CONTACT_NAME, contactName).commit();
					sharedPreference.edit()
					.putString(KEY_CONTACT_NUMBER, contactNo).commit();
//					Log.i("DREG", "NAME=" + contactName);
//					Log.i("DREG", "phoneNo=" + contactNo);
					// TODO Whatever you want to do with the selected contact
					// name.
				}
				c.close();
			}
			break;
		}

	}

	

}